We’ve finally arrived at creating our first maps based on the data we imported and cleaned in the previous sessions. The focus on this first session lies on the package “tmap” to create maps.
Let’s start super simple. Import our enhanced district data. Create a map with the outline of Germany and one were the borders of the districts are visible. Chose a color of your choice for both maps.
# load libraries
library(tmap)
library(sf)
library(dplyr)
# code based on the original
german_districts <- st_read(dsn = "./data",
layer = "GER_DISTRICTS") %>%
rename(., district_id = id)
attributes_districts <- read.csv("./data/attributes_districts.csv",
header = T, fill = T, sep = ",")
german_districts_enhanced <-
german_districts %>%
left_join(., attributes_districts, by = "district_id") %>%
st_transform(., crs = 3035)
# start by setting the tmap mode to "plot" for static maps
tmap_mode("plot") # interactive maps can be designed by changing the mode to "view"
# first maps based on geometrc features
tm_shape(german_districts_enhanced) + # call the shapefile first
tm_fill(col = "lightgrey") # fills the polygons without drawing borders
# ... or use tm_polygons
tm_shape(german_districts_enhanced) +
tm_polygons(col = "lightblue") # fills the polygons and draws borders
In a second step, we want to visualize some information on the German districts contained in the attribute table. Choose the column and create a map of Covid-19 cases per 100,000 inhabitants in Germany. Alternate the map by:
Assign your map to an object called “covid_map”.
Check the head() of your shapefile. You might to need to rename some of the columns or use the correct names: “cases_per_100k” (abbreviated: "cs__100").
Combine following options with a plus sign:
If you run “colors()”, R returns the names of all built-in colors.
To assign your map to an object use the arrow “<-”.
# plot the covid-19 cases per 100.000 inhabitants
tm_shape(german_districts_enhanced) +
tm_fill(col = "cases_per_100k") # "col" can be the name of a color or a column name
# change the title of the legend and the color palette
tm_shape(german_districts_enhanced) +
tm_fill(col = "cases_per_100k",
title = "Covid-19 Cases per 100k", # add a title to the legend
palette = "RdPu") # change the color palette
# add a title in the color blue
tm_shape(german_districts_enhanced) +
tm_fill(col = "cases_per_100k",
title = "Covid-19 Cases per 100k",
palette = "RdPu") +
tm_layout(title = "Covid Cases in Germany", # alternate the overall layout like title
title.color = "blue" ) # changes the font color of the title
# place the legend outside of the map to the left
tm_shape(german_districts_enhanced) +
tm_fill(col = "cases_per_100k",
title = "Covid-19 Cases per 100k",
palette = "RdPu") +
tm_layout(title = "Covid Cases in Germany",
title.color = "blue" ) +
tm_legend(legend.outside = TRUE, # positions the legend outside
legend.outside.position = "left") # defines legend positions
# save your map in an object called "covid_map"
covid_map <-
tm_shape(german_districts_enhanced) +
tm_fill(col = "cases_per_100k",
title = "Covid-19 Cases per 100k",
palette = "RdPu") +
tm_layout(main.title = "Covid Cases in Germany",
main.title.color = "blue" ) +
tm_legend(legend.outside = TRUE,
legend.outside.position = "left")